/**
 * 
 */
package talas;

import static robocode.util.Utils.normalRelativeAngleDegrees;
import java.awt.Color;
import robocode.*;

/**
 * @version 1.0
 * @author Brian Roldan
 * 
 */
public class TalasOne extends Robot {

	//legend T - targeting M - movement P - positioning
		private String target = null; // T M
		private int timeToCheck = 0;// T M
		private boolean toLeftTurn = false;// T M P
		private double wideScanRange = 60;// T
		private long lastScanTime = 0;//T M P
		private int shootCounter = 0;// T
		private double lastEnergy = 0;// M
		private boolean ahead = true;//M P
		private long wallTime = 0;// M
		private double maxHitPower = 3;// M
		
		
		public void run() {
				
			super.setColors(new Color(196,31,38), Color.WHITE, Color.WHITE, Color.WHITE, new Color(124,19,24));
			super.setAdjustGunForRobotTurn(true);
			super.turnRight(90);
			while(true) {
								
				//search
				if(this.timeToCheck <= 14){
					
					this.reset();
					
					if(this.toLeftTurn){
						super.turnGunLeft(190);
					}else{
						super.turnGunRight(190);
					}
					
				}
				
				
				
				
				if(this.timeToCheck == 15 || this.timeToCheck == 16){
					
					if(this.toLeftTurn){
						super.turnGunLeft(this.wideScanRange);					
					}else{
						super.turnGunRight(this.wideScanRange);
					}
					
					this.toLeftTurn = !this.toLeftTurn;
					
				}
				
				//keep decrementing until to search
				if(this.timeToCheck > 0)this.timeToCheck--;
								
			}
			
		}
		
		private void move(double bearing, double distance){
			
			
			
			if(distance > super.getBattleFieldWidth()/3){
				
				if(bearing -90 < 0)super.turnRight(bearing+30);
				else if(bearing -90 > 0)super.turnRight(bearing-30);
				
				this.ahead(super.getWidth()*3);
				
			}else{
							
				if(distance > 150){
						
					if(bearing -70 < 0)super.turnRight(bearing+70);
					else if(bearing -70 > 0)super.turnRight(bearing-70);
						
				}
				if(this.ahead)super.ahead(super.getWidth()*2);
				else super.back(super.getWidth()*2);
				
				
			}
			
		}
		
		private void reset(){
			
			this.target = null; // T M
			this.timeToCheck = 0;// T M
			this.toLeftTurn = false;// T M P
			this.wideScanRange = 45;// T
			this.lastScanTime = 0;//T M P
			this.shootCounter = 0;// T
			this.lastEnergy = 0;// M
			this.ahead = true;//M P
			this.wallTime = 0;// M
			this.maxHitPower = 3;// M
			
		}
		
		public void onScannedRobot(ScannedRobotEvent e) {
			
			if(this.target != null && !this.target.equals(e.getName())) return;
			
			if (this.target == null) this.target = e.getName();
			
			if(this.lastScanTime-System.currentTimeMillis() < 2000 && this.lastScanTime != 0){
				
				this.wideScanRange = 90;
			
			}else{
				
				this.wideScanRange = 60;
				
			}
			
			if(this.lastEnergy-e.getEnergy() > 0 && this.lastEnergy-e.getEnergy() <=this.maxHitPower ){
				this.move(e.getBearing(),e.getDistance());
				return;
			}
			
			this.lastScanTime = System.currentTimeMillis();
			this.timeToCheck = 16;
			this.toLeftTurn = !this.toLeftTurn;
			this.lastEnergy = e.getEnergy();
			
			double absoluteBearing = getHeading() + e.getBearing();
			double bearingFromGun = normalRelativeAngleDegrees(absoluteBearing - getGunHeading());
			
			if (Math.abs(bearingFromGun) <= 3) {
				turnGunRight(bearingFromGun);
				
				if (getGunHeat() == 0 && (super.getEnergy() > 10 || e.getEnergy() <= 0)){
					
					if(e.getDistance() < 10){
						
						super.fire(Math.min(3 - Math.abs(bearingFromGun), getEnergy() - .1)+2);
						
					}else{
						
						if(e.getDistance() > 100){
							
							if(this.shootCounter == 1 && super.getEnergy() > 15){
								super.turnGunRight(.5*+e.getVelocity());
							}
							if(this.shootCounter == 2 && super.getEnergy() > 15){
								super.turnGunLeft(.5*+e.getVelocity());
								
							}
						
						}
						if(this.shootCounter ==2)this.shootCounter = -2;
						this.shootCounter++;
						super.fire(Math.min(3 - Math.abs(bearingFromGun), getEnergy() - .1));
											
					}			
					
				}
				
			} 
			else {
				
				turnGunRight(bearingFromGun);
				
			}
							
		}
		
		public void onBulletHit(BulletHitEvent e) {}
		
		public void onBulletHitBullet(BulletHitBulletEvent e) { }
		
		public void onBulletMissed(BulletMissedEvent e) {}
		
		public void onHitByBullet(HitByBulletEvent e) { 
			
			if(this.maxHitPower < e.getPower())this.maxHitPower = e.getPower();
			
			double absoluteBearing = getHeading() + e.getBearing();
			double bearingFromGun = normalRelativeAngleDegrees(absoluteBearing - getGunHeading());
			
			
			super.turnGunRight(bearingFromGun);
			super.scan();
			
		}
		
		public void onRobotDeath(RobotDeathEvent e) {
			
			if(this.target != null && this.target.equals(e.getName())) this.reset();
			
		}
		
		public void onWin(WinEvent e) {
			
			while(true){
				
				this.turnRadarLeft(180);
				
			}		
			
		}
		
		public void onHitRobot(HitRobotEvent e) {
			
			double absoluteBearing = getHeading() + e.getBearing();
			double bearingFromGun = normalRelativeAngleDegrees(absoluteBearing - getGunHeading());
			super.turnGunRight(bearingFromGun);
			this.ahead = !this.ahead;
			scan();
			
		}
		
		public void onHitWall(HitWallEvent e) {
			
			this.ahead = !this.ahead;
			this.move(90, 0);
			if(this.wallTime - System.currentTimeMillis() > 20){
				
				super.turnRight(e.getBearing()-90);
				this.ahead(super.getHeight()/2);
				
			}
			
			this.wallTime = System.currentTimeMillis();
			
		}
	
}
